package com.xunk.equipment.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.xunk.common.core.utils.serialization.JsonSerializer;
import com.xunk.common.core.utils.serialization.UrlSerializer;
import com.xunk.common.core.web.domain.AjaxResult;
import com.xunk.equipment.service.mandun.constant.MandunConstant;
import com.xunk.equipment.service.mandun.service.impl.MandunAirSwitchNotifyServiceImpl;
import com.xunk.equipment.service.mandun.util.MandunUtil;
import com.xunk.equipment.service.mandun.vo.MandunPush;
import com.xunk.equipment.service.yikang.service.impl.DoorViewerNotifyService;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.util.Map;

/**
 * @ProjectName: xunk
 * @Package: com.xunk.equipment.controller
 * @ClassName: NotifyController
 * @Author: yangwendong
 * @Description: 设备回调接口 （ruying\mandun\...）
 * @Date: 2021/3/29 20:20
 * @Version: 1.0
 */
@Slf4j
@RestController
public class NotifyController extends BaseController {
    /**
     * 如影 回调设备已上线
     *
     * @param data
     */
    @PostMapping(value = "ruying/notify")
    public void ruyingEquipmentNotify(@RequestBody String data) {
        super.getEquipmentNotifyService("ruyingEquipmentNotifyService").equipmentNotify(data);
    }


    @PostMapping(value = "uplus/notify")
    public void uplusEquipmentNotify(@RequestBody(required = false) String data) {
        log.info("==================");
        log.info("data :  {}", data);
        log.info("==================");
    }


    /**
     * 移康猫眼推送
     *
     * @param param
     * @return
     */
    @PostMapping(value = "/event/notify")
    public AjaxResult doorViewer(@RequestBody Map<String, Object> param) {
        super.getEquipmentNotifyService(StrUtil.lowerFirst(DoorViewerNotifyService.class.getSimpleName())).equipmentNotify(JsonSerializer.serialize(param));
        return AjaxResult.success();
    }

    /**
     * 接收曼顿告警信息
     *
     * @param request
     * @return
     */
    @PostMapping(value = "mandun/receiveAlarm", headers = "content-type=application/x-www-form-urlencoded")
    public JSONObject receiveAlarm(HttpServletRequest request) {
        log.info("接收到曼顿告警推送");
        final String bodyString = MandunUtil.getBodyString(new HttpServletRequestWrapper(request));
        final Map<String, String> bodyMap = UrlSerializer.urlToMap(bodyString);
        final MandunPush mandunPush = JsonSerializer.deserialize(JsonSerializer.serialize(bodyMap), MandunPush.class);
        mandunPush.setType("receiveAlarm");
        //code	string	状态码: 0成功, 10密钥错误, 20服务器错误
        //msg	string	当detect等于0时，返回业务处理结果，如：成功。
        //当detect不等于0时，直接返回此数值即可。
        JSONObject jsonObject = new JSONObject();
        if (!MandunConstant.ZERO.equals(mandunPush.getDetect())) {
            jsonObject.put(MandunConstant.CODE, MandunConstant.ZERO);
            jsonObject.put(MandunConstant.MSG, mandunPush.getDetect());
            return jsonObject;
        }
        jsonObject.put(MandunConstant.MSG, mandunPush.getDetect());
        try {
            super.getEquipmentNotifyService(StrUtil.lowerFirst(MandunAirSwitchNotifyServiceImpl.class.getSimpleName())).equipmentNotify(JsonSerializer.serialize(mandunPush));
        } catch (Exception e) {
            e.getMessage();
            jsonObject.put(MandunConstant.CODE, MandunConstant.TWENTY);
            return jsonObject;
        }
        jsonObject.put(MandunConstant.CODE, MandunConstant.ZERO);
        return jsonObject;
    }

    /**
     * 接收曼顿设备实时信息
     *
     * @param request
     * @return
     */
    @PostMapping(value = "mandun/receiveRealTime", headers = "content-type=application/x-www-form-urlencoded")
    public JSONObject receiveRealTime(HttpServletRequest request) {
        log.info("接收到曼顿设备实时信息推送");
        final String bodyString = MandunUtil.getBodyString(new HttpServletRequestWrapper(request));
        final Map<String, String> bodyMap = UrlSerializer.urlToMap(bodyString);
        final MandunPush mandunPush = JsonSerializer.deserialize(JsonSerializer.serialize(bodyMap), MandunPush.class);
        mandunPush.setType("receiveRealTime");
        //code	string	状态码: 0成功, 10密钥错误, 20服务器错误
        //msg	string	当detect等于0时，返回业务处理结果，如：成功。
        //当detect不等于0时，直接返回此数值即可。
        JSONObject jsonObject = new JSONObject();
        if (!MandunConstant.ZERO.equals(mandunPush.getDetect())) {
            jsonObject.put(MandunConstant.CODE, MandunConstant.ZERO);
            jsonObject.put(MandunConstant.MSG, mandunPush.getDetect());
            return jsonObject;
        }
        jsonObject.put(MandunConstant.MSG, mandunPush.getDetect());
        try {
            super.getEquipmentNotifyService(StrUtil.lowerFirst(MandunAirSwitchNotifyServiceImpl.class.getSimpleName())).equipmentNotify(JsonSerializer.serialize(mandunPush));
        } catch (Exception e) {
            e.getMessage();
            jsonObject.put(MandunConstant.CODE, MandunConstant.TWENTY);
            return jsonObject;
        }
        jsonObject.put(MandunConstant.CODE, MandunConstant.ZERO);
        return jsonObject;
    }

    /**
     * 接收曼顿事件通知信息
     * 例如，设备新增删除等
     *
     * @param request
     * @return
     */
    @PostMapping(value = "mandun/receiveEvent", headers = "content-type=application/x-www-form-urlencoded")
    public JSONObject receiveEvent(HttpServletRequest request) {
        log.info("接收到曼顿事件消息推送");
        final String bodyString = MandunUtil.getBodyString(new HttpServletRequestWrapper(request));
        final Map<String, String> bodyMap = UrlSerializer.urlToMap(bodyString);
        final MandunPush mandunPush = JsonSerializer.deserialize(JsonSerializer.serialize(bodyMap), MandunPush.class);
        mandunPush.setType("receiveEvent");
        //code	string	状态码: 0成功, 10密钥错误, 20服务器错误
        //msg	string	当detect等于0时，返回业务处理结果，如：成功。
        //当detect不等于0时，直接返回此数值即可。
        JSONObject jsonObject = new JSONObject();
        if (!MandunConstant.ZERO.equals(mandunPush.getDetect())) {
            jsonObject.put(MandunConstant.CODE, MandunConstant.ZERO);
            jsonObject.put(MandunConstant.MSG, mandunPush.getDetect());
            return jsonObject;
        }
        jsonObject.put(MandunConstant.MSG, mandunPush.getDetect());
        try {
            super.getEquipmentNotifyService(StrUtil.lowerFirst(MandunAirSwitchNotifyServiceImpl.class.getSimpleName())).equipmentNotify(JsonSerializer.serialize(mandunPush));
        } catch (Exception e) {
            e.getMessage();
            jsonObject.put(MandunConstant.CODE, MandunConstant.TWENTY);
            return jsonObject;
        }
        jsonObject.put(MandunConstant.CODE, MandunConstant.ZERO);
        return jsonObject;
    }

}
